package fourfaith.com.ff_sl21_app;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import fourfaith.com.ff_sl21_app.utils.DataCleanManager;

public class ClearCacheActivity extends AppCompatActivity {
    private Button okBtn,cancelBtn;
    private OnDialogClickListener dialogClickListener;
    private Context context;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clear_cache);
        //添加Activity到容器，便于注销
        initViews();
        initListeners();
    }
    private void initViews() {
        initWindow();
        //实列化控件
        okBtn =(Button)findViewById(R.id.btn_ok);
        cancelBtn = (Button)findViewById(R.id.btn_cancel);
        context = this.getApplicationContext();

        //确定按钮的点击监听事件
        okBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //清除缓存
                DataCleanManager.cleanExternalCache(context);
                Toast.makeText(ClearCacheActivity.this, "清除缓存成功", Toast.LENGTH_SHORT).show();
                String cachePathSize = null;
                try {
                    cachePathSize = DataCleanManager.getCacheSize(context.getExternalCacheDir());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                TextView cachePath =  getFragmentManager().findFragmentById(R.id.userFragment).getView().findViewById(R.id.cacheSize);
                cachePath.setText(cachePathSize);
                finish();
                if (dialogClickListener != null) {
                    dialogClickListener.onCancelClick();
                }
            }
        });
    }
    private void initWindow() {
        //自定义windows弹窗的样式
        getWindow().setBackgroundDrawable(new ColorDrawable(0));
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN |
                WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
    }

    private void initListeners() {
        //取消按钮取点击监听
        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //关闭当前的activity
                finish();
                if (dialogClickListener != null) {
                    dialogClickListener.onCancelClick();
                }
            }
        });
    }

    public void setOnDialogClickListener(OnDialogClickListener clickListener){
        dialogClickListener = clickListener;
    }


    public interface OnDialogClickListener{
        void onOKClick();
        void onCancelClick();
    }

}
